using System.Collections;
using System.Collections.Generic;
using UnityEngine;
// using MFarm.Save;

public class Player : MonoBehaviour
{
    private Rigidbody2D rb;

    public float speed;
    private float inputX;
    private float inputY;

    private Vector2 movementInput;

    private Animator[] animators;

    private bool isMoving;

    private bool inputDisable;

    private float mouseX;
    private float mouseY;
    private bool useTool;

    private void Awake()
    {
        rb = GetComponent<Rigidbody2D>();
        animators = GetComponentsInChildren<Animator>();
        inputDisable = true;
    }

    private void Start()
    {
        inputDisable = false;
        // transform.position = Settings.playerStartPos;
    }
    // private void OnEnable()
    // {
    //     EventHandler.BeforeSceneUnloadEvent += OnBeforeSceneUnloadEvent;
    //     EventHandler.AfterSceneLoadEvent += OnAfterSceneLoadEvent;
    //     EventHandler.MoveToPosition += OnMoveToPosition;
    //     EventHandler.MouseClickedEvent += OnMouseClickedEvent;
    //     EventHandler.UpdateGameStateEvent += OnUpdateGameStateEvent;
    //     EventHandler.StartNewGameEvent += OnStartNewGameEvent;
    //     EventHandler.EndGameEvent += OnEndGameEvent;
    // }

    // private void OnDisable()
    // {
    //     EventHandler.BeforeSceneUnloadEvent -= OnBeforeSceneUnloadEvent;
    //     EventHandler.AfterSceneLoadEvent -= OnAfterSceneLoadEvent;
    //     EventHandler.MoveToPosition -= OnMoveToPosition;
    //     EventHandler.MouseClickedEvent -= OnMouseClickedEvent;
    //     EventHandler.UpdateGameStateEvent -= OnUpdateGameStateEvent;
    //     EventHandler.StartNewGameEvent -= OnStartNewGameEvent;
    //     EventHandler.EndGameEvent -= OnEndGameEvent;
    // }

    private void Update()
    {
        if (!inputDisable)
            PlayerInput();  // 处理玩家输入
        else
            isMoving = false;
        SwitchAnimation();  // 切换动画状态
    }
    private void FixedUpdate()
    {
        if (!inputDisable)
            Movement();  // 处理玩家移动
    }
    private void OnStartNewGameEvent(int index)
    {
        inputDisable = false;
        // transform.position = Settings.playerStartPos;
    }

    private void OnEndGameEvent()
    {
        inputDisable = true;
    }

    // private void OnUpdateGameStateEvent(GameState gameState)
    // {
    //     switch(gameState)
    //     {
    //         case GameState.GamePlay:
    //             inputDisable = false;
    //             break;
    //         case GameState.Pause:
    //             inputDisable = true;
    //             break;
    //     }
    // }

    // private void OnMouseClickedEvent(Vector3 mouseWorldPos, ItemDetails itemDetails)
    // {
    //     //TODO:??��???
    //     if (itemDetails.itemType != ItemType.Seed && itemDetails.itemType != ItemType.Commodity && itemDetails.itemType != ItemType.Furniture)
    //     {
    //         mouseX = mouseWorldPos.x - transform.position.x;
    //         mouseY = mouseWorldPos.y - (transform.position.y + 0.85f);

    //         if (Mathf.Abs(mouseX) > Mathf.Abs(mouseY))
    //             mouseY = 0;
    //         else
    //             mouseX = 0;

    //         StartCoroutine(UseToolRoutine(mouseWorldPos, itemDetails));
    //     }
    //     else
    //     {
    //         EventHandler.CallExecuteActionAfterAnimation(mouseWorldPos, itemDetails);
    //     }
    // }

    // private IEnumerator UseToolRoutine(Vector3 mouseWorldPos, ItemDetails itemDetails)
    // {
    //     useTool = true;
    //     inputDisable = true;
    //     yield return null;
    //     foreach (var anim in animators)
    //     {
    //         anim.SetTrigger("useTool");
    //         // 设置工具使用方向
    //         anim.SetFloat("InputX", mouseX);
    //         anim.SetFloat("InputY", mouseY);
    //     }
    //     yield return new WaitForSeconds(0.45f);
    //     EventHandler.CallExecuteActionAfterAnimation(mouseWorldPos, itemDetails);
    //     yield return new WaitForSeconds(0.25f);
    //     //???????????
    //     useTool = false;
    //     inputDisable = false;
    // }

    private void OnMoveToPosition(Vector3 targetPosition)
    {
        transform.position = targetPosition;
    }

    private void OnAfterSceneLoadEvent()
    {
        inputDisable = false;
    }

    private void OnBeforeSceneUnloadEvent()
    {
        inputDisable = true;
    }

    private void PlayerInput()  // 处理玩家输入控制
    {
        //if(inputY == 0)如果想限制移动轴向，不允许走斜线
        inputX = Input.GetAxisRaw("Horizontal");
        //if(inputY == 0)
        inputY = Input.GetAxisRaw("Vertical");

        if (inputX != 0 && inputY != 0)
        {
            inputX = inputX * 0.6f;
            inputY = inputY * 0.6f;
        }

        // 按住Shift键时降低移动速度
        if (Input.GetKey(KeyCode.LeftShift))
        {
            inputX = inputX * 0.5f;
            inputY = inputY * 0.5f;
        }

        movementInput = new Vector2(inputX, inputY);

        isMoving = movementInput != Vector2.zero;
    }

    private void Movement()  // 实现角色移动
    {
        rb.MovePosition(rb.position + movementInput * speed * Time.deltaTime);
    }

    private void SwitchAnimation()  // 更新动画状态
    {
        foreach (var anim in animators)
        {
            anim.SetBool("isMoving", isMoving);
            anim.SetFloat("mouseX", mouseX);
            anim.SetFloat("mouseY", mouseY);

            if (isMoving)
            {
                anim.SetFloat("InputX", inputX);
                anim.SetFloat("InputY", inputY);
            }
        }
    }
}
